using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.Designer.SCA
{
	public partial class FormSelectWebServices : DockContent
	{
		#region Initialization
		public FormSelectWebServices(IServiceProvider serviceProvider)
		{
			_serviceProvider = serviceProvider;

			InitializeComponent();

			dgvWebServices.AutoGenerateColumns = false;
		}
		#endregion

		#region Public Members
		#endregion

		#region Private Members

		#region Variables
		private Point _dragStart = Point.Empty;
		private IServiceProvider _serviceProvider;
		#endregion

		#region Methods
		private void getWebServices()
		{
			GenericFolder folder = Global.Data.GetRepositoryItemFolderOfType
				(ItemType.Domain, Global.Status.CurrentDomainId, ItemType.WebService);
			dgvWebServices.DataSource = Global.Data.GetRepositoryHierarchyChildRepositoryList
				(folder, ItemType.WebService);
			dgvWebServices.Sort(colName, ListSortDirection.Ascending);
		}
		#endregion

		#region Event Handlers

		private void FormSelectWebServices_Load(object sender, EventArgs e)
		{
			getWebServices();
		}

		private void dgvWebServices_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
		{
			RepositoryService repSvc = (RepositoryService)_serviceProvider.GetService(typeof(RepositoryService));
			//if ( repSvc == null )
				//throw new NullReferenceException("RepositoryService not available.");

			if ( e.RowIndex >= 0 && e.ColumnIndex >= 0 ) {
				if ( e.Button == MouseButtons.Left && _dragStart == Point.Empty ) {
					_dragStart = e.Location;
				} else if ( e.Button == MouseButtons.Left && _dragStart != Point.Empty ) {
					if ( Math.Abs(_dragStart.X - e.Location.X) > 5 ||
						 Math.Abs(_dragStart.Y - e.Location.Y) > 5 ) {
						DataRowView drv = dgvWebServices.Rows[e.RowIndex].DataBoundItem as DataRowView;
						if ( drv != null ) {
							WebService ws = repSvc.GetItem(ItemType.WebService, drv.Row) as WebService;
							if ( ws != null && ws.ID > 0 ) {
								dgvWebServices.DoDragDrop(new RepositoryItemDragDropContainer(ws, false),
														  DragDropEffects.Move | DragDropEffects.Link);
							}
						}
					}
				} else if ( e.Button != MouseButtons.Left ) {
					_dragStart = Point.Empty;
				}
			} else {
				_dragStart = Point.Empty;
			}
		}

		#endregion

		private void dgvWebServices_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex > -1 && e.ColumnIndex == colWebMethods.Index ) {
				RepositoryService repSvc = (RepositoryService)_serviceProvider.GetService(typeof(RepositoryService));
				DataRowView drv = dgvWebServices.Rows[e.RowIndex].DataBoundItem as DataRowView;
				if ( drv != null ) {
					WebService ws = repSvc.GetItem(ItemType.WebService, drv.Row) as WebService;
					if ( ws != null && ws.ID > 0 ) {
						FormShowWebMethods frm = new FormShowWebMethods(ws);
						frm.ShowDialog();
					}
				}
			}
		}

		#endregion
	}
}